﻿using System;
using System.Collections.Generic;
using System.Linq;
using Com.PhilChuang.Mobile.Apps.LocationSender.Model;
using Com.PhilChuang.Mobile.Apps.LocationSender.Model.Data;
using Com.PhilChuang.Mobile.Apps.LocationSender.Model.Services;
using Com.PhilChuang.Mobile.Apps.LocationSender.Model.Services.Impl;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Presenters;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.Pages;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewFacades;
using Com.PhilChuang.Pmvp.Ui.Presenters;
using Com.PhilChuang.Pmvp.Utils;
using Com.PhilChuang.Utils;
using Com.PhilChuang.Utils.Mobile;

namespace Com.PhilChuang.Mobile.Apps.LocationSender
{
	internal class AppServiceLocatorImpl : SilverlightServiceLocatorBase
	{
		private static readonly VersionInfo s_VersionInfo =
			new VersionInfo (
			"PhilChuang.com",
			"Location Sender",
			1, 0, 0, 0, "",
			DateTime.ParseExact ("05/01/2011", "MM/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo));

		public AppServiceLocatorImpl ()
		{
			CacheImpl (s_VersionInfo);

			PmvpWp7ServiceLocator.AddServices (this);
			PmvpUtilsWp7ServiceLocator.AddServices (this);

			// Data classes
			AddMapping (typeof (LocationSenderSettings), false);

			// App
			AddMapping (typeof (LocationSenderApp), true);
			AddMapping (typeof (IAppHelper), typeof (LocationSenderAppHelper), true);

			// DAOs
			AddMapping (typeof (ILocationSenderDao), typeof (LocationSenderDaoDiskImpl), true);

			// services
			AddMapping (typeof (IGeoCoordinateProvider), typeof (GeoCoordinateProviderImpl), true);
			AddMapping (typeof (IBingMapsCredentialProvider), typeof (BingMapsCredentialProviderImpl), true,
				t => new BingMapsCredentialProviderImpl ("visit bingmapsportal.com and get an AppKey"));

			// configuration
			CacheImpl (new AboutPresenterConfig
			           	{
			           		VersionInfo = s_VersionInfo,
			           		ResourceAssembly = System.Reflection.Assembly.GetExecutingAssembly (),
							HelpContentResourceStreamName = "Com.PhilChuang.Mobile.Apps.LocationSender.Help.About.html",
			           		AppWebsiteLink = "http://philchuang.com/cs/blogs/software/pages/location-sender.aspx",
			           	});
			CacheImpl (new SendFeedbackPresenterConfig
			           	{
			           		VersionInfo = s_VersionInfo,
			           		FeedbackEmailAddress = "yourapp@yourdomain.com",
			           		FeedbackEmailSubject = "Feedback for {0}".FormatWith (s_VersionInfo),
			           		FeedbackEmailBodyFormat =
			           			"-------------------------------------------\r\n" +
			           			"FEEDBACK\r\n" +
			           			"-------------------------------------------\r\n" +
			           			"{0}\r\n" + // user text goes here
			           			"-------------------------------------------\r\n" +
			           			"APPLICATION\r\n" +
			           			"-------------------------------------------\r\n" +
			           			s_VersionInfo.FullProductId + "\r\n" +
			           			"-------------------------------------------\r\n",
			           	});
			CacheImpl (new ReportBugPresenterConfig
			           	{
			           		VersionInfo = s_VersionInfo,
							FeedbackEmailAddress = "yourapp@yourdomain.com",
			           		FeedbackEmailSubject = "Bug report for {0}".FormatWith (s_VersionInfo),
			           		FeedbackEmailBodyFormat =
			           			"-------------------------------------------\r\n" +
			           			"WHAT I WAS DOING\r\n" +
			           			"-------------------------------------------\r\n" +
			           			"{0}\r\n" + // user text goes here
			           			"-------------------------------------------\r\n" +
			           			"APPLICATION\r\n" +
			           			"-------------------------------------------\r\n" +
			           			s_VersionInfo.FullProductId + "\r\n" +
			           			"-------------------------------------------\r\n" +
			           			"BUG\r\n" +
			           			"-------------------------------------------\r\n" +
			           			"{1}\r\n" + // bug text goes here
			           			"-------------------------------------------\r\n",
			           	});

			// views
			AddMapping (typeof (IWelcomeView), typeof (WelcomeViewFacade), false);
			AddMapping (typeof (ISelectContactsView), typeof (SelectContactsViewFacade), false);
			AddMapping (typeof (ISelectFormatView), typeof (SelectFormatViewFacade), false);
			AddMapping (typeof (ISettingsView), typeof (SettingsViewFacade), false);

			// pages
			CacheImpl (new NavigationHelper (
						new[]
			           		{
			           			new NavigationHelperMapping (
									typeof (WelcomePage), 
									"/LocationSenderAppWp7;component/Ui/Views/Impl/Pages/WelcomePage.xaml", 
									page => new WelcomePresenter (new WelcomeViewFacade ((WelcomePage) page))),
			           			new NavigationHelperMapping (
									typeof (SelectContactsPage), 
									"/LocationSenderAppWp7;component/Ui/Views/Impl/Pages/SelectContactsPage.xaml", 
									page => new SelectContactsPresenter (new SelectContactsViewFacade ((SelectContactsPage) page))),
			           			new NavigationHelperMapping (
									typeof (SelectFormatPage), 
									"/LocationSenderAppWp7;component/Ui/Views/Impl/Pages/SelectFormatPage.xaml", 
									page => new SelectFormatPresenter (new SelectFormatViewFacade ((SelectFormatPage) page))),
			           			new NavigationHelperMapping (
									typeof (SettingsPage), 
									"/LocationSenderAppWp7;component/Ui/Views/Impl/Pages/SettingsPage.xaml", 
									page => new SettingsPresenter (new SettingsViewFacade ((SettingsPage) page))),
			           		}));
			PmvpUtilsWp7ServiceLocator.AddNavigationHelperMappings (GetImpl<NavigationHelper> ());
		}
	}
}